package cn.itcast.service.cargo.impl;

import cn.itcast.dao.cargo.InvoiceDao;
import cn.itcast.dao.cargo.ShippingDao;
import cn.itcast.domain.cargo.Invoice;
import cn.itcast.domain.cargo.Shipping;
import cn.itcast.domain.cargo.ShippingExample;
import cn.itcast.service.cargo.InvoiceService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.UUID;

// com.alibaba.dubbo.config.annotation.Service;
@Service(timeout = 200000) // 设置超时时间为200秒
public class InvoiceServiceImpl implements InvoiceService {

    @Autowired
    private InvoiceDao invoiceDao;

    /**
     * 1.
     * 分页
     *
     * @param companyId
     * @return
     */
    @Override
    public PageInfo<Invoice> findByPage(String companyId, int pageNum, int pageSize) {
        //开启分页支持,自动对其后的第一个查询进行分页
        PageHelper.startPage(pageNum, pageSize);

        List<Invoice> invoiceList = invoiceDao.findAll(companyId);

        return new PageInfo<>(invoiceList);
    }

    /**
     * 2.
     * 根据发票id查询一个发票
     *
     * @param invoiceId
     * @return
     */
    @Override
    public Invoice findById(String invoiceId) {
        return invoiceDao.findById(invoiceId);
    }

    /**
     * 3.
     * 根据企业id查询所有发票
     *
     * @param companyId
     * @return
     */
    @Override
    public List<Invoice> findAll(String companyId) {
        List<Invoice> invoiceList = invoiceDao.findAll(companyId);
        return invoiceList;
    }

    @Autowired
    private ShippingDao shippingDao;

    /**
     * 4.
     * 添加一个发票
     */
    @Override
    public void save(Invoice invoice) {
        /**
         * 把id作为主键
         */
        invoice.setId(UUID.randomUUID().toString());

        invoice.setStatus(0);

        String shippingOrderId = invoice.getShippingOrderId();
        ShippingExample shippingExample = new ShippingExample();
        ShippingExample.Criteria criteria = shippingExample.createCriteria();
        criteria.andShippingOrderIdEqualTo(shippingOrderId);
        //Shipping shipping = shippingDao.selectByPrimaryKey(shippingOrderId);
        List<Shipping> shippings = shippingDao.selectByExample(shippingExample);
        if (shippings != null && shippings.size()>0){
            for (Shipping shipping : shippings) {
                shipping.setState(1);
                shippingDao.updateByPrimaryKeySelective(shipping);
            }
        }


        //修改委托的状态
     // Shipping shipping = new Shipping();
     // shipping.setState(1);
     // ShippingExample shippingExample = new ShippingExample();
     // shippingExample.createCriteria().andShippingOrderIdEqualTo(invoice.getShippingOrderId());
     // shippingDao.updateByPrimaryKeySelective(shipping);

        invoiceDao.insertSelective(invoice);
    }


    /**
     * 删除发票
     * 6.
     *
     * @param id
     */
    @Override
    public boolean delete(String id) {

        //判断状态是否为草稿
        Invoice invoice = invoiceDao.findById(id);
        if (invoice.getStatus() == 0) {

            invoiceDao.deleteByPrimaryKey(id);
            return true;
        }

        return false;
    }

}
